<!--
 * @Author: Aaron
 * @Date: 2022-02-18 10:02:44
 * @LastEditors: YanMeng
 * @LastEditTime: 2022-11-05 17:49:45
 * @Description: file content
 * @FilePath: \ias_ui\src\views\coi\components\BrandName.vue
 * @Company: 沃尔玛
-->
<template>
  <el-select
    :value="value"
    filterable
    remote
    :remote-method="remoteChannelBrand"
    no-data-text="无对应品牌信息"
    placeholder="请输入搜索关键字"
    size="mini"
    :disabled="disabled"
    clearable
    @change="change"
  >
    <el-option v-for="item in brandOptions" :key="item.channelBrandId" :label="item.channelBrandName" :value="item.channelBrandId" />
  </el-select>
</template>

<script>
import { queryChannelBrandList } from '@/api/coi/mapping/brand'
export default {
  name: 'BrandName',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    channelId: {
      type: [String, Number],
      required: true
    },
    value: {
      type: [Number, String, null],
      required: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'one'
    },
    channelInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      brandOptions: []
    }
  },
  methods: {
    // 搜索渠道品牌
    remoteChannelBrand(param) {
      if (param) {
        queryChannelBrandList({ channelBrandName: param, channelId: this.channelId }).then((res) => {
          if (res && res.result && res.result.list) {
            this.brandOptions = res.result.list
          }
        })
      }
    },
    change(val) {
      if (this.type === 'all') {
        let label = ''
        this.brandOptions.forEach((item) => {
          if (item.channelBrandId === val) {
            label = item.channelBrandName
          }
        })
        this.$emit('change', { value: val, label: label })
      } else {
        let label = ''
        this.brandOptions.forEach((item) => {
          if (item.channelBrandId === val) {
            label = item.channelBrandName
          }
        })
        this.channelInfo.channelBrandName = label
        console.log(this.channelInfo)
        this.$emit('change', val)
      }
    }
  }
}
</script>

<style></style>
